{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "池化层\n",
    "padding"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "二维最大池化层和平均池化层\n",
    "max(), mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mxnet import nd\n",
    "from mxnet.gluon import nn\n",
    "def pool2d(X, pool_size, mode='max'):\n",
    "    p_h, p_w = pool_size\n",
    "    Y = nd.zeros((X.shape[0] - p_h + 1, X.shape[1] - p_w + 1))\n",
    "    for i in range(Y.shape[0]):\n",
    "        for j in range(Y.shape[1]):\n",
    "            if mode == 'max':\n",
    "                Y[i, j] = X[i: i+p_h, j: j+p_w].max()\n",
    "            elif mode == 'avg':\n",
    "                Y[i, j] = X[i: i+p_h, j: j+p_w].mean()\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[4. 5.]\n",
       " [7. 8.]]\n",
       "<NDArray 2x2 @cpu(0)>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = nd.array([[0, 1, 2],\n",
    "             [3, 4, 5],\n",
    "             [6, 7, 8]])\n",
    "pool2d(X, (2, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[2. 3.]\n",
       " [5. 6.]]\n",
       "<NDArray 2x2 @cpu(0)>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d(X, (2, 2), 'avg')"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "填充和步幅"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[[[ 0.  1.  2.  3.]\n",
       "   [ 4.  5.  6.  7.]\n",
       "   [ 8.  9. 10. 11.]\n",
       "   [12. 13. 14. 15.]]]]\n",
       "<NDArray 1x1x4x4 @cpu(0)>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = nd.arange(16).reshape((1, 1, 4, 4))\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[[[10.]]]]\n",
       "<NDArray 1x1x1x1 @cpu(0)>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2D(3)\n",
    "pool2d(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[[[ 5.  7.]\n",
       "   [13. 15.]]]]\n",
       "<NDArray 1x1x2x2 @cpu(0)>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2D(3, padding=1, strides=2)\n",
    "pool2d(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[[[ 0.  3.]\n",
       "   [ 8. 11.]\n",
       "   [12. 15.]]]]\n",
       "<NDArray 1x1x3x2 @cpu(0)>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2D((2, 3), padding=(1,2), strides=(2,3))\n",
    "pool2d(X)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "多通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[[[ 0.  1.  2.  3.]\n",
       "   [ 4.  5.  6.  7.]\n",
       "   [ 8.  9. 10. 11.]\n",
       "   [12. 13. 14. 15.]]\n",
       "\n",
       "  [[ 1.  2.  3.  4.]\n",
       "   [ 5.  6.  7.  8.]\n",
       "   [ 9. 10. 11. 12.]\n",
       "   [13. 14. 15. 16.]]]]\n",
       "<NDArray 1x2x4x4 @cpu(0)>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = nd.concat(X, X+1, dim=1)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[[[ 5.  7.]\n",
       "   [13. 15.]]\n",
       "\n",
       "  [[ 6.  8.]\n",
       "   [14. 16.]]]]\n",
       "<NDArray 1x2x2x2 @cpu(0)>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pool2d = nn.MaxPool2D(3, padding=1, strides=2)\n",
    "pool2d(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
